/* GUI CONVERTED from rerpexc.p (converter v1.76) Mon Mar 25 04:04:47 2002 */
/* rerpexc.p - EXPLODE REPETITIVE SCHEDULE INTO WORK ORDERS             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.10.2.11 $                                                     */
/*V8:ConvertMode=Report                                                 */
/*             WITH COMPONENTS AND ROUTINGS  (CONTINUED)                */
/*             MOVED FROM RERPEX.P DUE TO R-CODE LIMIT                  */
/* REVISION: 7.3      LAST MODIFIED: 02/25/94   BY: pma *FM46*          */
/* REVISION: 7.3      LAST MODIFIED: 07/27/94   BY: emb *GK84*          */
/* REVISION: 7.5      LAST MODIFIED: 11/09/94   BY: tjs *J027*          */
/* REVISION: 7.4      LAST MODIFIED: 03/13/95   BY: pxe *F0MF*          */
/* REVISION: 8.5      LAST MODIFIED: 06/16/95   BY: rmh *J04R*          */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 03/20/98   BY: *J23R* Santhosh Nair */
/* REVISION: 8.6E     LAST MODIFIED: 08/11/98   BY: *H1L6* Thomas Fernandes */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan       */
/* REVISION: 9.1      LAST MODIFIED: 03/03/99   BY: *N00J* Russ Witt        */
/* REVISION: 9.1      LAST MODIFIED: 09/13/99   BY: *N014* Jeff Wootton     */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 08/29/00   BY: *N0PN* Mark Brown       */
/* REVISION: 9.1      LAST MODIFIED: 09/30/00   BY: *L0Y1* Kirti Desai      */
/* REVISION: 9.1      LAST MODIFIED: 11/13/00   BY: *N0TN* Jean Miller      */
/* Old ECO marker removed, but no ECO header exists *F0PN*               */
/* Revision: 1.10.2.10     BY: Irine D'Mello  DATE: 09/10/01  ECO: *M164*     */
/* $Revision: 1.10.2.11 $    BY: K Paneesh  DATE: 03/23/02  ECO: *N1F9*     */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */
{pxmaint.i}

{pxphdef.i wocmnrtn}

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE rerpexc_p_1 "Qty Open"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

define new shared variable use_op_yield  as logical no-undo.

define shared variable comp like ps_comp.
define shared variable wo_recno as recid.
define shared variable prev_status like wo_status.
define shared variable prev_qty like wo_qty_ord.
define shared variable site like ptp_site.
define shared variable rps_start like rps_due_date.
define shared variable rpsduedate like rps_due_date.
define shared variable rpsduedate1 like rps_due_date.
define shared variable site1 like rps_site.
define shared variable site2 like rps_site.
define shared variable part like rps_part.
define shared variable part1 like rps_part.
define shared variable prline like rps_line.
define shared variable prline1 like rps_line.
define shared variable last_site like rps_site.
define shared variable last_line like rps_line.
define shared variable last_part like rps_part.

define variable rps_recno as recid no-undo.
define variable i as integer no-undo.
define variable qty as decimal no-undo.
define variable lt_off like ps_lt_off no-undo.
define variable rpsnbr like mrp_nbr no-undo.
define variable open_qty like rps_qty_req column-label {&rerpexc_p_1} no-undo.
define variable desc2 like pt_desc2 no-undo.
define variable wolot like wo_lot no-undo.
define variable rps_recids as recid extent 7 no-undo.
define variable rps_quantities like rps_qty_req extent 7 no-undo.
define variable temp_date like rps_due_date no-undo.
define variable loop as integer no-undo.
define variable mfg_lead like pt_mfg_lead no-undo.
define variable bom_code like pt_bom_code no-undo.
define variable routing like pt_routing no-undo.
define variable ptstatus like pt_status no-undo.
define variable yield_pct like pt_yield_pct no-undo.
define variable rpsrecord like rps_record   no-undo.
define variable  v-op       like ro_op         no-undo.
define variable l_errorno  as  integer      no-undo.

define shared buffer gl_ctrl for gl_ctrl.
define buffer rpsmstr for rps_mstr.

{rescttdf.i "new shared"}

{mfdatev.i}

define new shared temp-table tt-routing-yields no-undo
   field tt-routing    like ro_routing
   field tt-op         like ro_op
   field tt-start      like ro_start
   field tt-end        like ro_end
   field tt-yield-pct  like ro_yield_pct
   index tt-routing is unique primary
   tt-routing
   tt-op
   tt-start.

for first mrpc_ctrl
   fields (mrpc_op_yield)
   no-lock:
end.

for first rps_mstr no-lock
   where rps_part     >= part       and rps_part     <= part1
     and rps_site     >= site1      and rps_site     <= site2
     and rps_line     >= prline     and rps_line     <= prline1
     and rps_due_date >= rpsduedate and rps_due_date <= rpsduedate1
   use-index rps_part:
end. /* for first rps_mstr */

repeat with frame b width 132 no-attr-space:

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame b:handle).

   if not available rps_mstr
   then
      leave.

   if last_part <> rps_part
      or last_site <> rps_site
      or last_line <> rps_line
   then do with frame b:

      assign
         yield_pct = 100
         mfg_lead = 0
         routing  = ""
         bom_code = "".

      if last_part <> rps_part
      then
         desc2 = "".

      for first pt_mstr
         fields(pt_abc pt_avg_int pt_cyc_int pt_ord_pol pt_rctpo_active
                pt_rctpo_status pt_rctwo_active pt_rctwo_status
                pt_bom_code pt_desc1 pt_desc2 pt_mfg_lead pt_part
                pt_op_yield pt_prod_line pt_routing pt_yield_pct)
         no-lock where pt_part = rps_part:
      end. /* for first pt_mstr */

      if available pt_mstr
      then do:

         assign
            yield_pct = pt_yield_pct
            use_op_yield = pt_op_yield
            mfg_lead = pt_mfg_lead
            routing  = pt_routing
            bom_code = pt_part.

         if pt_bom_code > ""
         then
            bom_code = pt_bom_code.
         if last_part <> rps_part
         then
            desc2 = pt_desc2.

      end. /* IF AVAILABLE PT_MSTR */

      for first ptp_det
         fields(ptp_bom_code ptp_mfg_lead ptp_part ptp_routing ptp_op_yield
                ptp_site ptp_yld_pct ptp_ord_pol)
         no-lock
         where ptp_part = rps_part
           and ptp_site = rps_site:
      end. /* for first ptp_det */

      if available ptp_det
      then do:

         assign
            yield_pct = ptp_yld_pct
            use_op_yield = ptp_op_yield
            mfg_lead = ptp_mfg_lead
            routing  = ptp_routing
            bom_code = ptp_part.

         if ptp_bom_code > ""
         then
            bom_code = ptp_bom_code.
      end.

      if last_part <> rps_part
      then do with frame b:
         display rps_part pt_desc1 when (available pt_mstr) rps_site
            rps_line WITH STREAM-IO /*GUI*/ .
      end.
      else display rps_site rps_line with frame b STREAM-IO /*GUI*/ .

      assign
         site = rps_site
         comp = ""
         last_line = rps_line
         last_site = rps_site
         last_part = rps_part.
   end.

   else if desc2 > ""
   then do with frame b:
      display desc2 @ pt_desc1 WITH STREAM-IO /*GUI*/ .
      desc2 = "".
   end.

   assign
      rps_recids = 0
      rps_quantities = 0
      temp_date = rps_due_date.

   if weekday(temp_date) = 1
   then
      temp_date = temp_date - 7.
   else
      temp_date = temp_date + 1 - weekday(temp_date).

   do i = 1 to 7:

      for first rps_mstr no-lock
         where rps_part     = last_part
           and rps_site     = last_site
           and rps_line     = last_line
           and rps_due_date = temp_date + i:
      end. /* for first rps_mstr */

      if not available rps_mstr
      then
         next.

      assign
         rps_recids[i] = recid(rps_mstr)
         rps_quantities[i] = max(rps_qty_req - max(rps_qty_comp,0),0).
   end.

   repeat loop = 1 to 7 with frame b:

      if rps_recids[loop] = 0
      then
         next.

      for first rps_mstr
         no-lock where recid(rps_mstr) = rps_recids[loop]:
      end. /* for first rps_mstr */

      assign
         open_qty = rps_quantities[loop]
         open_qty = max(open_qty,0).

      if rps_due_date < rps_start
      then
         open_qty = 0.

      if rps_due_date < rpsduedate
         or rps_due_date > rpsduedate1
      then
         next.

      {dateconv.i rps_due_date rpsnbr}

      rpsnbr = rpsnbr + rps_site.

      /* Changed pre-processor to Term: REPETITIVE_SCHEDULE */
      {mfmrw.i "rps_mstr" rps_part rpsnbr string(rps_record) """"
         rps_rel_date rps_due_date 0
         "SUPPLYF" REPETITIVE_SCHEDULE rps_site}

      {mfmrwdel.i "rps_scrap" rps_part rpsnbr
         string(rps_record) """"}

      for first wo_mstr
         fields(wo_acct wo_sub wo_bom_code wo_cc wo_due_date wo_flr_acct
                wo_flr_sub wo_flr_cc wo_line wo_lot wo_mvar_acct wo_mvar_sub
                wo_mvar_cc wo_mvrr_acct wo_mvrr_sub wo_mvrr_cc wo_nbr
                wo_ord_date wo_part wo_qty_comp wo_qty_ord wo_qty_rjct
                wo_rel_date wo_routing wo_site wo_status wo_svar_acct
                wo_svar_sub wo_svar_cc wo_svrr_acct wo_svrr_sub
                wo_svrr_cc wo_type wo_var wo_xvar_acct wo_xvar_sub
                wo_xvar_cc wo_yield_pct)
         no-lock
         where wo_lot  = string(rps_record)
           and wo_part = rps_part
           and wo_site = rps_site and wo_status = "E"
           and wo_line = rps_line
           and wo_type = "S":
      end. /* for first wo_mstr */

      if open_qty = 0
         and available wo_mstr
      then do:

         assign
            wo_recno = recid(wo_mstr)
            prev_status = wo_status
            prev_qty = wo_qty_ord.

         {gprun.i ""wowomtd.p""}
      end.

      if open_qty <> 0
         and not available wo_mstr
      then do:

         create wo_mstr.
         assign
            wo_part = rps_part
            wo_nbr = rps_part
            wo_type = "S"
            wo_status = "E"
            wo_line = rps_line
            wo_site = rps_site.

         for first woc_ctrl no-lock:
         end. /* for first woc_ctrl */

         if available woc_ctrl
         then
            wo_var = woc_var.

         if length(string(rps_record)) > 8
         then do:

            rps_recno = recid(rps_mstr).
            find rps_mstr
               where recid(rps_mstr) = rps_recno.

            rpsrecord =  integer(substring(string(rps_record),
                                 length(string(rps_record)) - 7,8)).
            do while
               can-find(rpsmstr where rpsmstr.rps_record = rpsrecord):

               rpsrecord = rpsrecord + 1.
            end.

            rps_record = rpsrecord.
         end.

         if can-find (wo_mstr where wo_lot = string(rps_record))
         then do:

            wolot = string(rps_record).
            do while
               can-find(rpsmstr where rpsmstr.rps_record = integer(wolot)):

               {mfnxtsq.i wo_mstr wo_lot woc_sq01 wolot}

               if recid(wo_mstr) = -1 then .

            end.

            rps_recno = recid(rps_mstr).
            find rps_mstr
               where recid(rps_mstr) = rps_recno.

            rps_record = integer(wolot).
         end.

         wo_lot = string(rps_record).

         /* ASSIGN DEFAULT RECEIPT STATUS AND ACTIVE FLAG */
         {pxrun.i &PROC    = 'get_default_wo_rctstat'
                  &PROGRAM = 'wocmnrtn.p'
                  &HANDLE  = ph_wocmnrtn
                  &PARAM   = "(
                               input  wo_part,
                               input  wo_site,
                               output wo_rctstat,
                               output wo_rctstat_active,
                               output l_errorno
                              )"
         }

         for first pt_mstr
            fields (pt_abc          pt_avg_int      pt_bom_code
                    pt_cyc_int      pt_desc1        pt_desc2
                    pt_insp_lead    pt_insp_rqd     pt_joint_type
                    pt_loc          pt_mfg_lead     pt_mrp
                    pt_network      pt_op_yield     pt_ord_max
                    pt_ord_min      pt_ord_mult     pt_ord_per
                    pt_ord_pol      pt_ord_qty      pt_part
                    pt_plan_ord     pt_pm_code      pt_prod_line
                    pt_pur_lead     pt_rctpo_active pt_rctpo_status
                    pt_rctwo_active pt_rctwo_status pt_routing
                    pt_sfty_time    pt_timefence    pt_um
                    pt_yield_pct)
            where pt_part = wo_part
            no-lock:
         end. /* FOR FIRST pt_mstr */

         /* ASSIGN DEFAULT VARIANCE ACCOUNT SUB-ACCOUNT */
         /* AND COST CENTER CODE.                       */
         run assign_default_wo_acct(buffer wo_mstr,
                                    input pt_prod_line).

         if recid(wo_mstr) = -1 then .

      end.

      if available wo_mstr
      then do:

         if length(wo_lot) > 8
         then do:

            wolot = wo_lot.
            {gprun.i ""rerpexd.p"" "(input-output wolot)"}

            for first wo_mstr
               fields(wo_acct wo_sub wo_bom_code wo_cc wo_due_date wo_flr_acct
                      wo_flr_sub wo_flr_cc wo_line wo_lot wo_mvar_acct
                      wo_mvar_sub wo_mvar_cc wo_mvrr_acct wo_mvrr_sub
                      wo_mvrr_cc wo_nbr wo_ord_date wo_part
                      wo_qty_comp wo_qty_ord wo_qty_rjct
                      wo_rel_date wo_routing wo_site wo_status wo_svar_acct
                      wo_svar_sub wo_svar_cc wo_svrr_acct wo_svrr_sub
                      wo_svrr_cc wo_type wo_var wo_xvar_acct wo_xvar_sub
                      wo_xvar_cc wo_yield_pct)
               no-lock where wo_lot = wolot:
            end. /* for first wo_mstr */

         end.

         wo_recno = recid(wo_mstr).

         for first wo_mstr
            fields(wo_acct wo_sub wo_bom_code wo_cc wo_due_date wo_flr_acct
                   wo_flr_sub wo_flr_cc wo_line wo_lot wo_mvar_acct wo_mvar_sub
                   wo_mvar_cc wo_mvrr_acct wo_mvrr_sub
                   wo_mvrr_cc wo_nbr wo_ord_date wo_part
                   wo_qty_comp wo_qty_ord wo_qty_rjct
                   wo_rel_date wo_routing wo_site wo_status wo_svar_acct
                   wo_svar_sub wo_svar_cc wo_svrr_acct wo_svrr_sub
                   wo_svrr_cc wo_type wo_var wo_xvar_acct wo_xvar_sub
                   wo_xvar_cc wo_yield_pct)
            where recid(wo_mstr) = wo_recno:
         end. /* for first wo_mstr */

         assign
            wo_ord_date = today
            wo_rel_date = rps_rel_date
            wo_due_date = rps_due_date.

         find in_mstr exclusive-lock
            where in_part = wo_part
              and in_site = wo_site no-error.

         if available in_mstr
         then do:

            if wo_qty_ord >= 0
            then
               in_qty_ord = in_qty_ord - max(wo_qty_ord - wo_qty_comp
                                             - wo_qty_rjct,0).
            else
               in_qty_ord = in_qty_ord - min(wo_qty_ord - wo_qty_comp
                                             - wo_qty_rjct,0).
         end.

         /* STORE THE CURRENT WO_QTY_ORD IN A WKFL SO THAT IT CAN BE USED IN  */
         /* LATER HIERARCHY OF PROGRAMS. THIS IS DONE SO AS TO AVOID CREATING */
         /* A NEW SHARED VARIABLE.                                            */

         for each t_wr_route
            where t_wr_lot = wo_lot
            exclusive-lock:

            delete t_wr_route.
         end.

         for each wr_route no-lock
            where wr_lot = wo_lot:

            if ((wo_qty_ord - wr_qty_ord) <> 0)
            then do:
               create t_wr_route.
               assign
                  t_wr_lot       = wr_lot
                  t_wr_op        = wr_op
                  t_wr_scrap     = wo_qty_ord - wr_qty_ord.
            end.

         end.

         assign
            wo_qty_ord  = rps_qty_req
            wo_qty_comp = max(rps_qty_comp,wo_qty_ord - open_qty).

         if wo_qty_rjct <> 0
         then
            wo_qty_comp = max(wo_qty_comp - wo_qty_rjct,0).

         if available in_mstr
         then do:

            if wo_qty_ord >= 0
            then
               in_qty_ord = in_qty_ord + max(wo_qty_ord - wo_qty_comp
                                             - wo_qty_rjct,0).
            else
               in_qty_ord = in_qty_ord + min(wo_qty_ord - wo_qty_comp
                                             - wo_qty_rjct,0).
         end.

         wo_bom_code = if bom_code <> wo_part
                       then
                          bom_code
                       else
                          "".

         wo_routing = routing.

         if rps_routing > ""
         then
            wo_routing = rps_routing.
         if rps_bom_code > ""
         then
            wo_bom_code = rps_bom_code.

         assign
            wo_yield_pct = yield_pct
            wo_rel_date = ?.

         {mfdate.i wo_rel_date wo_due_date mfg_lead wo_site}

         /* Determine if operation yield is needed, and if so override  */
         /* wo_yield_pct.                                               */

         /* if global switch for operational yield is no, set local     */
         /* switch to no                                                */

         if (available mrpc_ctrl and mrpc_op_yield = no)
            or not available mrpc_ctrl
         then
            use_op_yield = no.

         if use_op_yield
         then do:

            /* Load temp table with this item's routing data...  */
            run ip-load-routing-temp-table
               (input if wo_routing <> ""
                      then
                         wo_routing
                      else
                         wo_part).

            /* Calculate yield... */
            v-op = 999999999.
            run ip-get-yield
               (input if wo_routing <> ""
                      then
                         wo_routing
                      else
                         wo_part,
               input v-op,
               input wo_rel_date,
               output yield_pct).

            wo_yield_pct = yield_pct.

         end.  /* if use_op_yield... */

         if wo_rel_date <> rps_rel_date
         then do:

            rps_recno = recid(rps_mstr).
            find rps_mstr
               where recid(rps_mstr) = rps_recno
               exclusive-lock.

            rps_rel_date = wo_rel_date.
         end.

         /* Changed pre-processor to Term: REPETITIVE_SCHEDULE */
         {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
            wo_rel_date wo_due_date
            "max(wo_qty_ord - max(wo_qty_comp,0)
                     - max(wo_qty_rjct,0),0)"
            "SUPPLYF" REPETITIVE_SCHEDULE wo_site}

         /* Changed pre-processor to Term: SCRAP_REQUIREMENT */
         {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
            wo_rel_date wo_due_date
            "max(wo_qty_ord - max(wo_qty_comp,0)
                     - max(wo_qty_rjct,0),0)
             * (1 - wo_yield_pct / 100)"
            "DEMAND" SCRAP_REQUIREMENT wo_site}

         /* CREATE/VALIDATE WORK ORDER ROUTING */
         {gprun.i ""reworlc.p""}

         /* SCHEDULE OPERATION START AND DUE DATES */
         {gprun.i ""woworle.p""}

         /* CREATE WORK ORDER COMPONENTS */
         if comp = ""
            or (wo_bom_code <> comp
            and (wo_bom_code <> "" or comp <> bom_code))
         then do:

            if wo_bom_code > ""
            then
               comp = wo_bom_code.
            else
               comp = bom_code.

            {gprun.i ""rerpexb.p""}
         end.

         {gprun.i ""rerpexa.p""}
      end.

      else do:

         temp_date = ?.
         {mfdate.i temp_date rps_due_date mfg_lead rps_site}

         if temp_date <> rps_rel_date
         then do:

            rps_recno = recid(rps_mstr).
            find rps_mstr exclusive-lock
               where recid(rps_mstr) = rps_recno.

            rps_rel_date = temp_date.
         end.
      end.

      for each t_wr_route
         where t_wr_lot = wo_lot exclusive-lock:

         delete t_wr_route.
      end.

      /* Moved display down to here from above */
      display rps_rel_date rps_due_date
         rps_qty_req rps_qty_comp open_qty WITH STREAM-IO /*GUI*/ .
      down 1.

      if desc2 > ""
      then
         display desc2 @ pt_desc1 WITH STREAM-IO /*GUI*/ .
      desc2 = "".

   end.

   
/*GUI*/ {mfguichk.i } /*Replace mfrpchk*/


   find next rps_mstr no-lock
      where rps_part >= part and rps_part <= part1
        and rps_site >= site1 and rps_site <= site2
        and rps_line >= prline and rps_line <= prline1
        and rps_due_date >= rpsduedate and rps_due_date <= rpsduedate1
      use-index rps_part no-error.
end.

/******************************************************************/

/*   I N T E R N A L    P R O C E D U R E S     */

/******************************************************************/

/* This routine will load routing information  for a part into  */
/* a temp table.                                                */
{gplodyld.i}

/******************************************************************/

/* This routine will determine operation yield percentage        */
/* used.                                                         */
{gpgetyld.i}

/******************************************************************/

/* INCLUDE FILE WHICH CONTAINS PROCEDURE assign_default_wo_acct */
{woacct.i}
